import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;

public class GetEarningReportTick {    

    private static String input = new String();
    private static Vector vInput= new Vector(10);
    private static Vector vOut = new Vector(10);

    public static void main(String[] args) {
	String url = "http://biz.yahoo.com/research/earncal/20020424.html";
	String urlOutputS="";
	Vector urlOutputV = new Vector(10);
	GetEarningReportTick myReport = new GetEarningReportTick();

	myReport.connectToUrl(url);
	urlOutputS = myReport.getUrlOutput();
	urlOutputS = myReport.formatUrlOutput(urlOutputS);
	//System.out.print(urlOutput);
	urlOutputV = myReport.putInVectorByLine(urlOutputS);

	//myReport.printVector(vInput);
	urlOutputV = myReport.selectUsefulInfo(urlOutputV);
	//myReport.printVector(urlOutputV);
	myReport.printSelectedElement(urlOutputV, 1); 
    }


   private void connectToUrl (String urlAddress) {
        URL url = null;        
	HttpURLConnection httpConnection = null;
	String up;
	//up =  encodeAuthorization();

	try {            

	    url = new URL (urlAddress);  

	    try {                
		httpConnection = (HttpURLConnection)url.openConnection();                
		httpConnection.setDoOutput(false);                
		httpConnection.connect();                

		// get the HTTP server's response code                
		System.out.println("Connection Code and Message:" + 
				   httpConnection.getResponseCode() + 
				   " - " + httpConnection.getResponseMessage()+"\n");

                // get the input stream of the connection                
		// ie. read the response from the HTTP server
                
		BufferedReader in = new BufferedReader(new InputStreamReader
		    (httpConnection.getInputStream()));

		StringBuffer buff1= new StringBuffer("");

		input = in.readLine();

		while (input != null) {
		    buff1.append(input);
		    input = in.readLine();
		}

		in.close();            

		input =  buff1.toString();

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	    System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    public String getUrlOutput() {
	return input;
    }

    public String formatUrlOutput (String inString) {
	String outString = inString;
	outString=outString.replaceAll(">",">\n");
	outString=outString.replaceAll("</","\n</");
	outString=outString.replaceAll("<","\n<");
	//outString=outString.replaceAll("&nbsp;","\nN/A");
	//outString=outString.replaceAll("<br>","== \n");
	return outString;
    }

    private Vector putInVectorByLine (String input) {

	Vector v = new Vector (10);
	String element = "";
	StringTokenizer tokens = new StringTokenizer(input, "\n");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		v.addElement(element);
	    }
	}
	
	return v;
    }

    private Vector selectUsefulInfo (Vector v_in) {
	Vector v_out = new Vector (10);
	String text = "";

	Enumeration penum = v_in.elements();

	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	    if (text.startsWith("Call")) {
		break;
	    }
	}

	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	    if (!text.startsWith("<")) {
		if (text.startsWith("ADVERTISEMENT")) {
		    break;
		    }
		if(!(text.equals("Add") ||text.equals("Listen"))) {
		    text = text.replaceAll("&nbsp;","");
		    text = text.replaceAll("&amp;","&");
		    v_out.addElement(text);
		}
	    }
	}

	return v_out;
    }

    private void printUsefulInfo (String input) {
	System.out.println(input);
    }

    private void printVector( Vector inputV) {
	String temp ="";
	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {
	    temp =(String)penum.nextElement();
	    System.out.println(temp);
	}
    }

    private void printSelectedElement(Vector v_in, int which) {
	String temp =""; int n = which; int m =0;
	Enumeration penum = v_in.elements();
	while ( penum.hasMoreElements() ) {

	    temp =(String)penum.nextElement();

	    if (m ==4) m=0;
	    
	    if (temp.equals("\n") || temp.equals("")) {
		m=-1;
	    }
	   
	    if (m == n) {
		System.out.println(temp);
	    }
	    m++;
	}
    }
    private void printSambolSid( Vector inputV) {
	String temp = "";
	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {
	    temp = (String)penum.nextElement();
	    temp = temp.replace('?', ' ');
	    temp = temp.replaceAll("<A HREF=","");
	    temp = temp.replaceAll("/custom/datek-com/screener/screener-charting.asp", "");
	    temp = temp.replaceAll("symb=","");
	    temp = temp.replaceAll("\"","");
	    temp = temp.replaceAll("&sid=", "\t");
	    temp = temp.replaceAll(">","");  
	   System.out.println(temp.trim());
	}
    }

 
    private static String encodeAuthorization () {
	// method to encode the username/password

	String authorization = "LI34276" + ":" + "********";
	sun.misc.BASE64Encoder enCoder = new sun.misc.BASE64Encoder();
	return "Basic " + enCoder.encode(authorization.getBytes());
    }
}






